//
//  CJExten+CALayer.swift
//  CJCore
//
//  Created by 陈警卫 on 2024-06-05.
//

import UIKit
import Foundation

public extension CAGradientLayer {
    
    /// 设置渐变色
    @objc func cj_gradientLayer(colors: [Any], locations: [NSNumber]?, startPoint: CGPoint, endPoint: CGPoint) -> CAGradientLayer {
        self.colors = colors
        self.locations = locations
        self.startPoint = startPoint
        self.endPoint = endPoint
        return self
    }
}

public extension CALayer {
    
    /// 移除子视图
    @objc func cj_removeAllSublayers() {
        guard let sublayers = self.sublayers else { return }
        for layer in sublayers {
            layer.removeFromSuperlayer()
        }
    }
    
    
    /// 基础旋转动画
    @discardableResult
    @objc func cj_basicRotationAnimation(key: String = "cj_basicRotationAnimation", duration: CFTimeInterval = 1.5, isRemove: Bool = false) -> CABasicAnimation {
        
        let rotationAnimation = CABasicAnimation(keyPath: key)
        rotationAnimation.toValue = NSNumber(value: Double.pi * 2.0)
        rotationAnimation.duration = duration
        rotationAnimation.isCumulative = true
        rotationAnimation.repeatCount = MAXFLOAT
        rotationAnimation.isRemovedOnCompletion = false
        
        if isRemove {
            self.removeAnimation(forKey: key)
        } else {
            self.add(rotationAnimation, forKey: key)
        }
        return rotationAnimation
    }
    
    /// 弹性动画
    @discardableResult
    @objc func cj_springAnimation(keyPath: String?,
                            key: String = "cj_springAnimation",
                            fromValue: Any?,
                            toValue: Any?,
                            delay: CFTimeInterval = 0,
                            mass: CGFloat = 5.0,
                            stiffness: CGFloat = 1000.0,
                            damping: CGFloat = 10.0,
                            initialVelocity: CGFloat = 10.0,
                            isRemovedOnCompletion: Bool = true,
                            option: CAMediaTimingFunctionName = .default) -> CASpringAnimation {
        
        let springAnimtion = CASpringAnimation(keyPath: keyPath)
        // 延时
        springAnimtion.beginTime = CACurrentMediaTime() + delay
        // 附着到弹簧末端的对象的质量
        springAnimtion.mass = mass
        // 弹簧刚度系数
        springAnimtion.stiffness = stiffness
        // 阻尼系数
        springAnimtion.damping = damping
        // 初始速率
        springAnimtion.initialVelocity = initialVelocity
        // settlingDuration 弹簧系统所需的估计持续时间
        springAnimtion.duration = springAnimtion.settlingDuration
        
        springAnimtion.fromValue = fromValue
        springAnimtion.toValue = toValue
        springAnimtion.isRemovedOnCompletion = isRemovedOnCompletion
        
        springAnimtion.fillMode = CAMediaTimingFillMode.forwards
        // 动画的时间节奏控制方式
        springAnimtion.timingFunction = CAMediaTimingFunction(name: option)
        self.add(springAnimtion, forKey: key)
        
        return springAnimtion
    }
    
}
